var cfg = require('../../utils/cfg');
var sendRequest = require('../../utils/requestUtils');
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
      orders : []
  },

  preImg : function (event) {
    let that = this;
    let picUrl = event.currentTarget.dataset.picUrl;
    let orderId = event.currentTarget.dataset.orderId;
    let orders = that.data.orders;
    let urls = [];
    for (let i=0; i<orders.length; i++) {
      if (orders[i].id == orderId) {
        if (orders[i].picUrl1 != "") {
          urls.push(orders[i].picUrl1);
        }
        if (orders[i].picUrl2 != "") {
          urls.push(orders[i].picUrl2);
        }
        if (orders[i].picUrl3 != "") {
          urls.push(orders[i].picUrl3);
        }
      }
    }
    wx.previewImage({
      current: picUrl,
      urls: urls
    })
  },

  overOrder : function (event) {
    let that = this;
    let orderId = event.currentTarget.dataset.orderId;
    wx.showModal({
      title: '提示',
      content: '确定已回收？',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          sendRequest.sendReq(cfg.djurl.overOrder, {
            orderId : orderId,
            openid : app.globalData.openId
          }, function (result) {
              console.log(result.data);
              that.loadOrders();
          });
        }
      }
    })
  },

  callCustomer : function (event) {
    let phone = event.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },

  loadOrders : function () {
    let that = this;
    //loadOrder
    sendRequest.sendReq(cfg.djurl.loadOrder, {
      openid : app.globalData.openId
    }, function (result) {
        console.log(result.data);
        that.setData({
          orders: result.data
        })
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadOrders();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})